import { ChangeDetectorRef, Component, ViewChild, ViewRef, OnInit } from '@angular/core';
import { STColumn, STComponent, STData, STPage, STRes } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { ModalHelperOptions } from '@delon/theme/src/services/modal/modal.helper';
import format from 'date-fns/format';
import * as _ from 'lodash';
import * as moment from 'moment';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { zip } from 'rxjs';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'app-conf-host-state',
  templateUrl: './state.component.html'
})
export class ConfHostStateComponent implements OnInit {
  i: any;
  hostInfo: any;
  hostStates: any;
  loading = true;
  total = 0;
  pi = 1;
  ps = 5;
  listOfData = [];
  success_state_flags = [
    'shop_conf_has_exists', //# 店铺配置文件存在
    'cookie_has_exists', // cookie文件存在
    'cookie_no_expired', // cookie未过期（已更新）
    'schtasks_has_exists', // 计划任务已存在
    'shop_password_correct', // 店铺密码正确
    'host_port_correct', // 服务器端口正确
    'host_password_correct', // 服务器密码正确
    'host_username_correct' // 服务器用户名正确
  ];
  error_state_flags = [
    'shop_conf_no_exists', //# 店铺配置文件不存在
    'cookie_no_exists', // cookie文件不存在
    'cookie_has_expired', // cookie已过期（未更新）
    'schtasks_no_exists', // 计划任务不存在
    'shop_password_incorrect', // 店铺密码不正确
    'host_port_incorrect', // 服务器端口错误
    'host_password_incorrect', // 服务器密码错误
    'host_username_incorrect' // 服务器用户名错误
  ];

  public st!: this;

  constructor(
    private modal: NzModalRef,
    private modal2: ModalHelper,
    private modal3: NzModalService,
    private msgSrv: NzMessageService,
    private http: _HttpClient,
    private cdr: ChangeDetectorRef
  ) {}

  ngOnInit(): void {
    if (this.i.orderSn != '') {
      this.loadDataFromServer(this.i.ID, this.i.host_ip, this.i.account_username);
    }
    this.cdr.detectChanges();
  }

  loadDataFromServer(ID: number, host_ip: string, account_username: string) {
    zip(this.http.get(`api/host/${ID}`, {}), this.http.get(`api/host/states`, { account_username: account_username, host_ip: host_ip }))
      .pipe(
        finalize(() => {
          this.cdr.detectChanges();
        })
      )
      .subscribe(([itemHost, itemStates]: [NzSafeAny, NzSafeAny]) => {
        this.hostInfo = itemHost.data;
        this.hostStates = itemStates?.data;
        this.listOfData = this.hostStates.list;
        this.loading = false;
      });
  }

  getStateColor(state_flag: string): string {
    return this.success_state_flags.indexOf(state_flag) >= 0 ? 'text-success' : 'text-error';
  }

  hostReboot(host_ip: string): void {
    this.http.post(`api/host/reboot`, { host_ip: host_ip }).subscribe((res: NzSafeAny) => {});
    this.msgSrv.success(`已对ip：${host_ip}发出重启指令，请稍等几分钟后查看重启结果`);
  }

  close(): void {
    this.modal.destroy();
  }
}
